﻿using ClassTableClient.CustomControl;
using ClassTableClient.DTO;
using ClassTableClient.Enum;
using ClassTableClient.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ClassTableClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void NePropertBtn_Click(object sender, EventArgs e)
        {
            //控件个数
            int controlCount = GetContrlCount(this.PorpertPl);
            Propert propert = new Propert();
            propert.AutoScroll = false;
            propert.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            propert.Location = new Point(0, 55 * controlCount);

            propert.Parent = this.PorpertPl;
        }


        /// <summary>
        /// 获取该元素内部控件数量
        /// </summary>
        /// <param name="control">容器控件</param>
        /// <returns></returns>
        public static int GetContrlCount(Control control)
        {
            int num = 0;
            foreach (Control c in control.Controls)
            {
                if (c is Propert)//获取的控件类型
                {
                    num++;
                }
            }
            return num;
        }

        /// <summary>
        /// 一键生成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CreateBtn_Click(object sender, EventArgs e)
        {
            string tableName = this.ClassTxt.Text;
             int serverType = this.SqlTypeCbx.SelectedIndex + 1;
            List<GenericClassDTO> properts = new List<GenericClassDTO>();
            foreach (Control c in this.PorpertPl.Controls)
            {
                if (c is Propert)//获取的控件类型
                {
                    GenericClassDTO propert = new GenericClassDTO();
                    propert.PropertType = c.Controls["PropertTypeCbx"].Text;
                    propert.PropertName = c.Controls["PropertNameTxt"].Text;
                    propert.PropertRemark = c.Controls["PropertRemarkTxt"].Text;
                    properts.Add(propert);
                }
            }
            int resultCode = ClassServiceHelper.CreateClassFileAndSql(tableName, properts, (ServerTypeEnum)serverType, out string sql);
            this.SqlTxt.Text = sql;
        }

        /// <summary>
        /// 删除字段
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClearPorpertBtn_Click(object sender, EventArgs e)
        {
            this.PorpertPl.Controls.Remove(this.PorpertPl.Controls[this.PorpertPl.Controls.Count - 1]);
        }
    }
}
